package xyz.hubery.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import xyz.hubery.domain.UserBank;
import xyz.hubery.dto.UserBankDTO;
import xyz.hubery.feign.UserBankServiceFeign;
import xyz.hubery.model.R;
import xyz.hubery.service.UserBankMappers;
import xyz.hubery.service.UserBankService;
import xyz.hubery.vo.BindBankRequest;

@RestController
@RequestMapping("/userBanks")
@Api(tags = "会员的银行卡管理")
public class UserBankController implements UserBankServiceFeign {

    @Autowired
    private UserBankService userBankService;

    @GetMapping
    @ApiOperation(value = "分页查询用户的银行卡")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "usrId", value = "会员的 Id"),
            @ApiImplicitParam(name = "current", value = "当前页"),
            @ApiImplicitParam(name = "size", value = "每页显示的条数")
    })
    @PreAuthorize("hasAuthority('user_bank_query')")
    public R<Page<UserBank>> findByPage(Page<UserBank> page, Long usrId) {
        page.addOrder(OrderItem.desc("last_update_time"));
        Page<UserBank> userBankPage = userBankService.page(page, new LambdaQueryWrapper<UserBank>().eq(usrId != null, UserBank::getId, usrId));
        return R.ok(userBankPage);
    }


    @PostMapping("/status")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "银行卡的 Id"),
            @ApiImplicitParam(name = "status", value = "银行卡的状态"),
    })
    @ApiOperation(value = "修改银行卡的状态")
    public R updateStatus(Long id, Byte status) {
        UserBank userBank = new UserBank();
        userBank.setId(id);
        userBank.setStatus(status);
        boolean updateById = userBankService.updateById(userBank);
        if (updateById) {
            return R.ok();
        }
        return R.fail("银行卡状态修改失败");
    }

    @PatchMapping
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userBank", value = "银行卡的 json 数据"),
    })
    @ApiOperation(value = "修改银行卡")
    public R update(@RequestBody @Validated UserBank userBank) {
        boolean updateById = userBankService.updateById(userBank);
        if (updateById) {
            return R.ok();
        }
        return R.fail("银行卡修改失败");
    }


    @GetMapping("/current")
    @ApiOperation(value = "获取当前用户的银行卡信息")
    public R<UserBank> getUserBank() {
        Long userId = Long.valueOf(SecurityContextHolder.getContext().getAuthentication().getPrincipal().toString());
        UserBank userBank = userBankService.geyByUserId(userId);
        return R.ok(userBank);
    }

    @PostMapping("/bind")
    @ApiOperation("绑定银行卡")
    public R<Boolean> bind(@RequestBody @Validated BindBankRequest bankRequest) {
        Long userId = Long.valueOf(SecurityContextHolder.getContext().getAuthentication().getPrincipal().toString());
        boolean isOk = userBankService.addUserBank(userId,bankRequest);
        if(isOk) {
            return R.ok();
        }
        return R.fail();
    }


    /**
     * 查询用户银行卡信息
     * @return
     */
    @GetMapping("/info")
    @Override
    public UserBankDTO getCurrentUserInfo() {
        Long userId = Long.valueOf(SecurityContextHolder.getContext().getAuthentication().getPrincipal().toString());
        UserBank userBank = userBankService.geyByUserId(userId);
        return UserBankMappers.INSTANCE.entityToDto(userBank);
    }
}
